﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CmpEdu
{
    public partial class Frm10_2Main : Form
    {
        public Frm10_2Main()
        {
            InitializeComponent();
        }

        private void Frm10_2Main_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“userManagerDataSet.Admin”中。您可以根据需要移动或删除它。
            // 将数据集DataSet添加到数据提供器DataAdapter中
            this.adminTableAdapter.Fill(this.userManagerDataSet.Admin);

            //设置数据表格控件的外观样式
            DataGridAdmin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            DataGridAdmin.AutoResizeColumns();

            this.Text = "用户管理";                         //表格设置
            DataGridAdmin.AllowUserToAddRows = false;
            DataGridAdmin.Columns[0].ReadOnly = true;
            DataGridAdmin.Columns[1].ReadOnly = true;
            DataGridAdmin.Columns[2].ReadOnly = true;

            if (FrmT10_2Login.UserLevel == "0")             //非管理员不可以增删
            {
                LbDel.Visible = false;
                LbDelAll.Visible = false;
                LbRead.Visible = false;
            }
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LbDel_Click(object sender, EventArgs e)
        {
            string DelName = CbName.Text.Trim();
            string MsgText = UserManager.DelUser(DelName);
            MessageBox.Show(MsgText, "删除反馈", MessageBoxButtons.OK, MessageBoxIcon.Information);
            adminTableAdapter.Fill(userManagerDataSet.Admin);     //刷新
        }

        private void LbDelAll_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("选中的记录将被删除，请确认", "提示",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                int RowMax = DataGridAdmin.RowCount;
                for (int i = 0; i < RowMax; i++)
                {
                    // 判断复选框是不是被选中
                    if ((bool)DataGridAdmin.Rows[i].Cells[3].EditedFormattedValue)
                    {
                        // 判断是不是管理员账户
                        if (DataGridAdmin.Rows[i].Cells[0].Value.ToString().Trim() != "admin")
                        {
                            string DelName = DataGridAdmin.Rows[i].Cells[0].Value.ToString().Trim();
                            UserManager.DelUser(DelName);
                        }
                        else
                        {
                            MessageBox.Show("默认管理员不可以被删除！", "出错",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
                adminTableAdapter.Fill(userManagerDataSet.Admin);
            }
        }

        private void LbRead_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("user.txt", Encoding.GetEncoding("gb2312"));
            try
            {
                while (!sr.EndOfStream)
                {
                    string NewVal1 = sr.ReadLine();
                    string NewVal2 = sr.ReadLine();
                    if (UserManager.CheckUser(NewVal1))
                    {
                        UserManager.AddUser(NewVal1, NewVal2, "0");
                    }
                    else
                    {
                        MessageBox.Show("添加失败：用户名" + NewVal1 + "已存在", "出错",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                MessageBox.Show("数据读取完毕！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                sr.Close();
            }
            adminTableAdapter.Fill(userManagerDataSet.Admin);
        }

        private void LbWrite_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"D:\out.txt",false,Encoding.Default);
            int RowCount = DataGridAdmin.RowCount;
            int ColumnCount = DataGridAdmin.ColumnCount;
            try
            {
                for (int row = 0; row < RowCount; row++)
                {
                    for (int col = 0; col < ColumnCount - 1; col++)
                    {
                        sw.Write(DataGridAdmin.Rows[row].Cells[col].Value.ToString() + "      ");
                    }
                    sw.WriteLine("");
                }
            }
            finally
            {
                MessageBox.Show("数据写入D:\\out.txt中", "提示", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                sw.Close();
            }
        }
    }
}
